const {
	add,
	list,
} = require('@utils/util-apis')['userUrl']

module.exports = function initUserPage() {
	let {
		userComp,
	} = $['components-jq']
	userComp.dailogComp = {
		formDialog: $("#user-form-dialog"),
	}
	userComp.dataGrid = $("#main-tabs-user")
	userComp.userForm = $("#user-form")
	userComp.userAccount = $("#user-form-account")
	userComp.userEmail = $("#user-form-email")
	userComp.userPassword = $("#user-form-pwd")
	userComp.userNickName = $("#user-form-nickname")
	userComp.userRealName = $("#user-form-realname")
	userComp.formBtnSave = $("#user-form-btn-save")
	userComp.formBtnCancel = $("#user-form-btn-cancel")
	initDailog(userComp)
	initDataGrid(userComp)
}

function initDailog(userComp) {
	let {
		dataGrid,
		userForm,
		dailogComp,
		formBtnSave,
		formBtnCancel,
		userAccount,
		userEmail,
		userPassword,
		userRealName,
	} = userComp
	let {
		formDialog,
	} = dailogComp
	formBtnSave.linkbutton({
		text: '保存',
		iconCls: 'icon-save',
		onClick() {
			$.messager.progress();
			userForm.form('submit', {
				url: add,
				onSubmit: function() {
					let isValid = userForm.form('validate');
					if (!isValid) {
						$.messager.progress('close');
					}
					return isValid;
				},
				success: function() {
					$.messager.progress('close');
					dataGrid.datagrid('reload');
					formDialog.dialog('close');
					userForm.form('clear')
				}
			})

		},
	})
	formBtnCancel.linkbutton({
		text: '取消',
		iconCls: 'icon-cancel',
		onClick() {
			userForm.form('clear')
			formDialog.dialog('close')
		},
	})
}

function initDataGrid(userComp) {
	let {
		dataGrid,
		dailogComp,
	} = userComp
	let {
		formDialog,
	} = dailogComp
	dataGrid.datagrid({
		fit: true,
		fitColumns: true,
		rownumbers: true,
		singleSelect: true,
		toolbar: [{
			iconCls: 'icon-add',
			text: '新建',
			handler() {
				formDialog.dialog('open')
			}
		}, '-', {
			iconCls: 'icon-edit',
			text: '修改',
			handler() {
				console.log(this)
			}
		}, '-', {
			iconCls: 'icon-remove',
			text: '删除',
			handler() {
				alert('help')
			}
		}],
		url: list,
		columns: [
			[{
				field: 'id',
				title: 'ID',
				width: 20
			}, {
				field: 'account',
				title: '用户名',
				width: 80
			}, {
				field: 'realname',
				title: '真实姓名',
				width: 80,
			}, {
				field: 'nickname',
				title: '昵称',
				width: 80,
			}, {
				field: 'email',
				title: '邮箱',
				width: 80,
			}, {
				field: 'create_date',
				title: '创建时间',
				width: 80,
			}, {
				field: 'last_login_date',
				title: '最近登录时间',
				width: 80,
			}]
		],
	})

	// $.get('/ult/user/list', function(result) {
	// 	console.log('result', result)
	// })
}